package com.golaberto.client;

import com.golaberto.client.WorkPanel.WorkPanelInfo;                  
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasAlignment;                              
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GolAberto implements EntryPoint, HistoryListener {

  private DockPanel content_panel_ = new DockPanel();
  protected WorkPanelList list_ = new WorkPanelList();
  private WorkPanelInfo cur_info_ = null;
  private WorkPanel cur_panel_ = null;

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    loadPanels();

    content_panel_.setWidth("100%");
    content_panel_.setHeight("100%");

    final DockPanel main_panel = new DockPanel();

    main_panel.add(list_, DockPanel.WEST);
    main_panel.add(content_panel_, DockPanel.CENTER);

    main_panel.setCellVerticalAlignment(list_, HasAlignment.ALIGN_TOP);
    main_panel.setCellWidth(content_panel_, "100%");
    main_panel.setCellHeight(content_panel_, "100%");

    History.addHistoryListener(this);
    RootPanel.get("main").add(main_panel);

    // Show the initial screen.
    String initToken = History.getToken();
    if (initToken.length() > 0) {
      onHistoryChanged(initToken);
    } else {
      show(list_.find("Championships"), false);
    }
  }

  /**
   * Adds all panels to the list. Note that this does not create actual
   * instances of all panels yet (they are created on-demand). This can make a
   * significant difference in startup time.
   */
  protected void loadPanels() {    
    list_.addWorkPanel(Championships.init());
    list_.addWorkPanel(Players.init());
    list_.addWorkPanel(Teams.init());
  }

  public void show(WorkPanelInfo info, boolean affectHistory) {
    // Don't bother re-displaying the existing panel. This can be an issue
    // in practice, because when the history context is set, our
    // onHistoryChanged() handler will attempt to show the currently-visible
    // panel.
    if (info == cur_info_)
      return;
    cur_info_ = info;

    // Remove the old panel from the display area.
    if (cur_panel_ != null) {
      cur_panel_.onHide();
      content_panel_.remove(cur_panel_);
    }

    // Get the new panel instance, and display its description in the
    // panel list.
    cur_panel_ = info.getInstance();
    list_.setWorkPanelSelection(info.getName());
    //description.setHTML(info.getDescription());

    // If affectHistory is set, create a new item on the history stack. This
    // will ultimately result in onHistoryChanged() being called. It will call
    // show() again, but nothing will happen because it will request the exact
    // same panel we're already showing.
    if (affectHistory)
      History.newItem(info.getName());

    // Display the new panel.
    content_panel_.add(cur_panel_, DockPanel.CENTER);
    content_panel_.setCellWidth(cur_panel_, "100%");
    content_panel_.setCellHeight(cur_panel_, "100%");
    content_panel_.setCellVerticalAlignment(cur_panel_, DockPanel.ALIGN_TOP);
    cur_panel_.onShow();
  }

  public void onHistoryChanged(String historyToken) {
    // This method is called whenever the application's history changes. Set
    // the label to reflect the current history token.
    WorkPanelInfo info = list_.find(historyToken);
    if (info == null) {
      return;
    }
    show(info, false);
  }
}
